//
//  UserInfoModel.m
//  AJBracelet
//
//  Created by zorro on 15/7/6.
//  Copyright (c) 2015年 zorro. All rights reserved.
//

#import "UserInfoModel.h"
#import "UserInfoHelper.h"
#import "YYJSONHelper.h"

@implementation UserInfoModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _birthday = @"2017-01-01";
        _nickname = @"";
        _interest  = @"";
        _manifesto = @"";
        
        _gender = 2;
        _height = 175;
        _weight = 70;
        _lanuage        = 0;
        _isMetricSystem = NO;
        _is12Time       = NO;
        
        _target_step  = 10000;
        _target_sleep = 480;
        
        _heartMin = 50;
        _heartMax = 100;
        
        _bloxyMin     = 50;
        _bloxyMax     = 100;
        _physicalTime = 60;
        
        _portrait            = @"";
        _lastMacAddress      = @"";
        _lastBleUuid         = [AJ_LastWareUUID getObjectValue];
        _autoCheckHeart      = YES;
        _changeScreen        = 0;
        _messageNoticeSwitch = 0;
        _hand                = NO;
        NSInteger bmr = 0;
        if (_gender ==1) {
            bmr = 10*_weight +6.25*_height - 5* _age.integerValue +5;
        }
        else
        {
            bmr = 10*_weight +6.25*_height - 5* _age.integerValue -161;
        }
        bmr = (bmr/100+1)*100;
        _goal_calory        = bmr;
    }
    
    return self;
}

+ (UserInfoModel *)parseString:(NSString *)string {
    UserInfoModel *user = [string toModel:[self class]];
    return user;
}

- (void)updateDataSafely {
    NSString      *where = [NSString stringWithFormat:@"user_id = '%@'", _user_id];
    UserInfoModel *model = [UserInfoModel searchSingleWithWhere:where orderBy:nil];
    
    if (!model) {
        model         = [[UserInfoModel alloc] init];
        model.user_id = _user_id;
        [model saveToDB];
    } else {
        [UserInfoModel updateToDB:self where:nil];
    }
}

+ (UserInfoModel *)getUserInfoFromDB {
    NSString      *where = [NSString stringWithFormat:@"userName = '%@'", AJ_UserName];
    UserInfoModel *model = [UserInfoModel searchSingleWithWhere:where orderBy:nil];
    
    if (!model) {
        model = [[UserInfoModel alloc] init];
        [model saveToDB];
    }
    
    return model;
}

+ (UserInfoModel *)getUserInfoFromDBWithUserID:(NSString *)userID {
    NSString      *where = [NSString stringWithFormat:@"user_id = '%@'", userID];
    UserInfoModel *model = [UserInfoModel searchSingleWithWhere:where orderBy:nil];
    
    if (!model) {
        model         = [[UserInfoModel alloc] init];
        model.user_id = userID;
        [model saveToDB];
    }
    return model;
}

// 显示公制 1为公制 0 为英制
- (NSString *)showMetricSystem {
    if (_isMetricSystem) {
        return @"1";
    } else {
        return @"0";
    }
}

// 1 24小时制度 0为12小时制度
- (NSString *)showAllDays {
    if (_is12Time) {
        return @"1";
    } else {
        return @"0";
    }
}

// 计算年龄
- (NSString *)age {
    NSDate    *date  = [NSDate dateByString:_birthday];
    NSDate    *today = [NSDate date];
    NSInteger isFull = 0;
    
    if (today.month * 100 + today.day > date.month * 100 + date.day) {
        isFull = 1;
    }
    
    return StrByInt(today.year - date.year + isFull);
}

// 0中文 1英文
- (NSString *)showLanuage {
    int type = 0;
    
    if (_lanuage == 0) {
        type = 0;
    } else {
        type = 1;
    }
    
    return [NSString stringWithFormat:@"%d", type];
}

- (NSString *)showGenderSex {
    return (_gender == 1) ? HHH_Text(@" Female ") : HHH_Text(@"  Male ");
}

- (NSString *)showAge {
    return [NSString stringWithFormat:@"%d", _gender];
}

- (NSString *)showHeight {
    if ([SAVEENTERUSERKEY getBOOLValue]) {
        return [NSString stringWithFormat:@"%d", _height];
    } else {
        if (_gender) {
            return @"160";
        } else {
            return @"170";
        }
    }
}

- (NSString *)showWeight {
    if ([SAVEENTERUSERKEY getBOOLValue]) {
        return [NSString stringWithFormat:@"%d", _weight];
    } else {
        if (_gender) {
            return @"50";
        } else {
            return @"70";
        }
    }
}

- (NSString *)portrait {
    return _portrait ? _portrait : @"";
}

- (NSString *)email {
    return _email ? _email : @"";
}

- (NSString *)currentlanguage {
    NSString *languageValue = @"0";
    
    NSArray  *appLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
    NSString *languageName = [appLanguages objectAtIndex:0];
    
    NSLog(@"系统语言>>>%@", languageName);
    
    if ([languageName isEqualToString:@"zh-Hans-CN"]) {
        languageValue = @"0";
    } else if ([languageName isEqualToString:@"en-CN"]) {
        languageValue = @"1";
    }
    
    return languageValue;
}

- (void)uploadToServer {
}

- (NSInteger)targetSteps {
    NSInteger steps = 0;
    
    for (int i = 0; i < self.targetStepsArray.count; i++) {
        NSString *number = self.targetStepsArray[i];
        steps += number.integerValue;
    }
    
    return steps;
}

- (NSInteger)targetSleep {
    NSInteger sleep = 0;
    
    for (int i = 0; i < self.targetSleepArray.count; i++) {
        NSString  *sting = self.targetSleepArray[i];
        NSArray   *array = [sting componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":-"]];
        NSInteger number = StrToInt(array[0]) * 60 + StrToInt(array[1]) + StrToInt(array[2]) * 60 + StrToInt(array[3]);
        sleep += number;
    }
    
    return sleep;
}

- (NSArray *)targetStepsArray {
    if (_targetStepsArray.count != 7) {
        _targetStepsArray = @[@"6000", @"6000", @"6000",
                              @"6000", @"6000", @"6000", @"6000"];
    }
    
    return _targetStepsArray;
}

- (NSArray *)targetSleepArray {
    if (_targetSleepArray.count != 7) {
        _targetSleepArray = @[@"12:00-12:00", @"12:00-12:00", @"12:00-12:00",
                              @"12:00-12:00", @"12:00-12:00", @"12:00-12:00", @"12:00-12:00"];
    }
    
    return _targetSleepArray;
}

// 表名
+ (NSString *)getTableName {
    return @"UserInfoModel";
}

// 主键
+ (NSString *)getPrimaryKey {
    return @"user_id";
}

// 表版本
+ (int)getTableVersion {
    return 1;
}

+ (void)initialize {
    //remove unwant property
    //比如 getTableMapping 返回nil 的时候   会取全部属性  这时候 就可以 用这个方法  移除掉 不要的属性
    
    [super initialize];
    [self bindYYJSONKey:@"id" toProperty:@"user_id"];
    
    [self removePropertyWithColumnName:@"avatar"];
    [self removePropertyWithColumnName:@"headImage"];
    [self removePropertyWithColumnName:@"interest"];
    [self removePropertyWithColumnName:@"manifesto"];
    [self removePropertyWithColumnName:@"headImage"];
    
    [self removePropertyWithColumnName:@"showGenderSex"];
    [self removePropertyWithColumnName:@"showHeight"];
    [self removePropertyWithColumnName:@"showWeight"];
}

@end

